home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-06-09 | 7.1 KB | 333 lines | [TEXT/MPS ] |
- {------------------------------------------------------------------------------
- #
- # SimpleKey
- # - a sample MIDI Management Tools application
- #
- # SimpleKey.p - Pascal Source
- #
- # Copyright © 1988-1989 Apple Computer, Inc.
- # All rights reserved.
- #
- # Version: 1.0
- #
- # SimpleKey is a sample application that uses the MIDI Management
- # Tools. It displays a small piano keyboard on the screen and
- # interprets mouse clicks to send MIDI events. It also has a
- # scroll bar for changing the channel that these events are sent on.
- #
- # Please be familiar with both the MIDI Management Tools and MIDI
- # in general before trying to understand this code.
- #
- # This application is an example of how to call the MIDI Management
- # tools. It is not meant as an example of a good Macintosh
- # application; it is NOT a template. It is NOT intended to be
- # used as a foundation for the next world-class, best-selling,
- # 600K application. A stick figure drawing of the human body may
- # be a good example of the form for a painting, but that does not
- # mean it should be used as the basis for the next Mona Lisa.
- #
- # This code also makes use of a small library called OneDialog.
- # This is a small set of routines that are only useful for very
- # simple applications. Again, it isn't a good general library.
- # You probably don't want to use it.
- #
- ------------------------------------------------------------------------------}
-
- PROGRAM SimpleKey;
-
- USES
- MemTypes,
- QuickDraw,
- OSIntf,
- ToolIntf,
- PackIntf,
- MIDI,
- OneDialog;
-
- CONST
- DialogRes = 500;
-
- { Dialog Item Numbers }
- NullItem = 0;
- QuitItem = 1;
- SilenceItem = 2;
- KeyboardItem = 3;
- ChannelItem = 4;
- ChannelTextItem = 5;
-
- mySignature ='mgL9';
-
- myIcon = 128;
-
- VAR
- Channel : INTEGER;
- PortRefNum : INTEGER;
- OurMenu : MenuHandle;
-
-
-
- PROCEDURE ReportOS(err: OSErr; who: Str255);
- VAR
- errNumber : Str255;
- BEGIN
- IF err <> NoErr THEN BEGIN
- NumToString(err, errNumber);
- Message(CONCAT(who, ': ', errNumber), ChannelTextItem);
- END;
- END;
-
-
-
- PROCEDURE WriteNote(note: INTEGER; onoff: BOOLEAN);
- VAR
- output : MIDIPacket;
- BEGIN
- WITH output DO BEGIN
- flags := midiMsgType + midiTimeStampCurrent + midiNoCont;
- len := 9;
- tStamp := 0;
-
- IF onoff THEN
- data[0] := $90 + Channel - 1
- ELSE
- data[0] := $80 + Channel - 1;
- data[1] := note;
- data[2] := 64;
- END;
-
- ReportOS(MIDIWritePacket(PortRefNum, @output), 'MIDIWritePacket');
- END;
-
-
-
-
- PROCEDURE SilenceAll;
- VAR
- n : INTEGER;
- output : MIDIPacket;
- BEGIN
- WITH output DO BEGIN
- flags := midiMsgType + midiTimeStampCurrent + midiNoCont;
- len := 9;
- tStamp := 0;
-
- data[0] := $B0 + Channel - 1;
- data[1] := 123;
- data[2] := 0;
- END;
-
- ReportOS(MIDIWritePacket(PortRefNum, @output), 'MIDIWritePacket');
-
- FOR n := 0 TO 127 DO
- WriteNote(n, FALSE);
- END;
-
-
- PROCEDURE Keyhit;
- VAR
- itemType : INTEGER;
- itemHdl : Handle;
- itemRect : Rect;
- pointHit : Point;
- width, height : INTEGER;
- octave, note : INTEGER;
- oldNote : INTEGER;
- BEGIN
- GetDItem(TheDialog, KeyboardItem, itemType, itemHdl, itemRect);
- oldNote := -1;
-
- WHILE StillDown DO BEGIN
- GetMouse(pointHit);
- octave := (pointHit.h - itemRect.left) DIV 49;
- IF (itemRect.bottom - pointHit.v) < 12 THEN BEGIN
- note := ((pointHit.h - itemRect.left) - (49 * octave)) DIV 7;
- note := note * 2;
- IF note >= 4 THEN note := note - 1;
- IF note >= 9 THEN note := note - 1;
- END ELSE BEGIN
- note := ((pointHit.h - itemRect.left) - (49 * octave) - 3) DIV 11;
- note := note * 2 + 1;
- IF note >= 3 THEN note := note + 1;
- IF note >= 8 THEN note := note + 1;
- END;
-
- note := note + 12 * octave + 21;
- IF note <> oldNote THEN BEGIN
- IF oldNote <> -1 THEN
- WriteNote(oldNote, FALSE);
- WriteNote(note, TRUE);
- oldNote := note;
- END;
- END;
-
- IF oldNote <> -1 THEN
- WriteNote(oldNote, FALSE);
- END;
-
-
-
-
- PROCEDURE SetChannel;
- VAR
- ChannelText : Str255;
- BEGIN
- DetermineValue(Channel, ChannelItem);
- NumToString(Channel, ChannelText);
- Message(CONCAT('Channel: ', ChannelText), ChannelTextItem);
- END;
-
-
-
- PROCEDURE StartUp;
- VAR
- init : MIDIPortParams;
- BEGIN
- IF SndDispVersion(midiToolNum) <> 0 THEN
- Message('MIDI Manager isn''t there!', ChannelTextItem);
- { This should really quit gracefully here
- and not call any other MIDI Manager Functions. }
-
- ReportOS(MIDISignIn(mySignature,
- 0,
- GetResource('ICN#', myIcon),
- 'Simple Keyboard'),
- 'MIDISignIn');
-
- WITH init DO BEGIN
- portID := 'out ';
- portType := midiPortTypeOutput;
- timeBase := 0;
- offsetTime := midiGetNothing;
- readHook := NIL;
- refCon := 0;
- name := 'notes';
- END;
- ReportOS(MIDIAddPort(mySignature, 300, PortRefNum, @init), 'AddDataPort');
-
- ClearMenuBar;
- OurMenu := GetMenu(128);
- InsertMenu(OurMenu, 0);
- AddResMenu(OurMenu, 'DRVR');
- DrawMenuBar;
-
- END;
-
-
-
- PROCEDURE ShutDown;
- BEGIN
- DeleteMenu(128);
- DisposHandle(Handle(OurMenu));
- DrawMenuBar;
-
- MIDISignOut(mySignature);
- END;
-
-
-
- {--- Dialog Management Routines ---}
- PROCEDURE StartDialog;
- VAR
- item : INTEGER;
- whoCares : BOOLEAN;
-
- BEGIN
- TheDialog := GetNewDialog(DialogRes, NIL, Pointer(-1));
- SetPort(TheDialog);
-
- FixScrollBar(ChannelItem, 1, 1);
- SetChannel;
- END;
-
-
-
- PROCEDURE RunDialog;
- VAR
- anEvent : EventRecord;
- itemHit : INTEGER;
- done : BOOLEAN;
- whichWindow : WindowPtr;
- boundry : Rect;
- item : LONGINT;
- daName : Str255;
- daRefNum : INTEGER;
- BEGIN
- ShowWindow(TheDialog);
-
- done := FALSE;
- REPEAT
- IF WaitNextEvent(everyEvent, anEvent, 50, NIL) THEN
- {IF GetNextEvent(everyEvent, anEvent) THEN}
- IF IsDialogEvent(anEvent) THEN
- IF DialogSelect(anEvent, TheDialog, itemHit) THEN
- CASE itemHit OF
- QuitItem: done := TRUE;
- SilenceItem: SilenceAll;
- KeyboardItem: Keyhit;
- ChannelItem: SetChannel;
-
- OTHERWISE
- SysBeep(2);
- END
- ELSE BEGIN END
- ELSE
- CASE FindWindow(anEvent.where, whichWindow) OF
- inMenuBar:
- BEGIN
- item := MenuSelect(anEvent.where);
- IF HiWrd(item) = 128 THEN BEGIN
- GetItem(GetMHandle(128), LoWrd(item), daName);
- daRefNum := OpenDeskAcc(daName);
- END;
- HiLiteMenu(0);
- END;
-
- inSysWindow:
- SystemClick(anEvent, whichWindow);
-
- inGoAway:
- IF TrackGoAway(whichWindow, anEvent.where) THEN
- done := TRUE;
-
- inDrag:
- BEGIN
- WITH screenBits.bounds DO
- SetRect(boundry, 4, 24, right - 4, bottom - 4);
- DragWindow(whichWindow, anEvent.where, boundry);
- END;
- END
- UNTIL done;
- END;
-
-
-
- PROCEDURE StopDialog;
- BEGIN
- HideWindow(TheDialog);
- DisposDialog(TheDialog);
- END;
-
-
-
- PROCEDURE InitThings;
- BEGIN
- InitGraf(@thePort);
- InitFonts;
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs(NIL);
- FlushEvents(everyEvent, 0);
- InitCursor;
- END;
-
-
-
- BEGIN
- InitThings;
- StartDialog;
- StartUp;
- RunDialog;
- ShutDown;
- StopDialog;
- END.